from django.db import models


# Create your models here.
class Category(models.Model):
    name = models.CharField(unique=True, max_length=50, verbose_name="类名")

    class Meta:
        db_table = "book_category"
        verbose_name = '书籍类别'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return self.name


class BookInfo(models.Model):
    title = models.CharField(max_length=200, verbose_name="书名", null=False)
    number = models.IntegerField(default=0, verbose_name="数量", null=True)
    author = models.CharField(max_length=200, verbose_name="作者")
    category = models.ForeignKey('books.Category', on_delete=models.CASCADE, related_name='book_category',
                                 verbose_name='类别')
    cover = models.CharField(max_length=200, verbose_name="图片", null=True)
    intro = models.TextField(max_length=20000, verbose_name="描述", null=True)

    class Meta:
        db_table = "book_info"
        verbose_name = '书籍信息'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return self.title


class BookHistory(models.Model):
    book = models.ForeignKey('books.BookInfo', on_delete=models.CASCADE, related_name='books',
                             verbose_name='图书名称')
    user = models.ForeignKey('users.User', on_delete=models.CASCADE, related_name="user", verbose_name="读者姓名")
    begin = models.DateField(verbose_name="借阅日期", auto_now_add=True)
    end = models.DateField(verbose_name="规划日期", null=True)
    status = models.BooleanField(default=False, verbose_name="是否归还")

    class Meta:
        db_table = "book_history"
        verbose_name = '借阅历史'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.book.name}——{self.userid.username}"
